object SinaDayMin5{
  def getSinaDay(ids:Seq[String],t:Int=10)={
    var ret=Array.empty[String]
    for(times <- 1 to t;if(ret.size==0)){
    try{
      val url=new java.net.URL("http://hq.sinajs.cn/list="+ids.mkString(","))
      val urlCon = url.openConnection()
      urlCon.setConnectTimeout(1500)
      urlCon.setReadTimeout( 500 )
      val tt=io.Source.fromInputStream(urlCon.getInputStream)
      ret=tt.getLines.toArray
      tt.close
      //println(times)
    }catch{
      case a:Exception=>
    }}
    ret.filter(a=>a.contains(",")).map{line=>
      val a=line.split("\"|,")
      val id=a(0).substring(11,11+8)
      val time=(a(31)+a(32)).replaceAll(":|\\-","")
      val volume=a(9).toLong
      val amount=a(10).toDouble*1000+0.4 toLong
      val lastclose=a(3).toDouble*1000+0.4 toLong
      val rr=time +: Seq(a(2),a(5),a(6),a(4)).map(_.toDouble*1000+0.4 toLong) :+ volume :+ amount :+ lastclose
      (id,rr.mkString(","))
    }.toMap
  }
  def getSinaDayConcurrent(stocks:Seq[String]):Map[String,String]={
    val ret=scala.collection.mutable.Map.empty[String,String]
    import scala.actors.Actor._
    val groups=stocks.grouped(150).toList
    val caller = self
    groups.foreach{id=>
      actor {
        caller ! getSinaDay(id)
      }
    }

    for(i <- 1 to groups.size)
    receive {
      case lines:Map[String,String] => ret ++= lines
    }
    ret.toMap
  }
  lazy val dateFormat=new java.text.SimpleDateFormat("yyyyMMddHHmmss")
  def getSinaDay(date:Long,date_sina:Long,stocks1:Seq[String])={
    val t=java.lang.System.currentTimeMillis
  	println(date)
    val date1=dateFormat.parse(date.toString).getTime+8000
    var f=false
    var stocks=stocks1
    if(date1>t)Thread.sleep(date1-t)
    val size=10
    for(i <- 1 to size;if(!f)){
      val t=java.lang.System.currentTimeMillis
      val r=getSinaDayConcurrent(stocks).filter(!_._2.contains("0,0,0,0,0,0,"))
      stocks=r.filter(_._2.take(8+6).toLong<(date)).toList.map(_._1)
      f=(stocks.size==0)
      println(i)
      val rr=if(i==size) r else r.filter(_._2.take(8+6).toLong>=date)
      rr.foreach(a=>save("SinaDayMin5/"+date_sina.toString.take(8)+"/"+a._1+".txt",Seq(date_sina+","+a._2),true))
      val t1=java.lang.System.currentTimeMillis
      if(!f && (date1+3000*i>t1))Thread.sleep(date1+3000*i-t1)
    }
  }
  def save(file:String,s:TraversableOnce[String],append:Boolean=false){
    val pdir=new java.io.File(file).getParentFile
    if(pdir!=null && !pdir.exists) pdir.mkdirs
    val filestream=new java.io.FileOutputStream(file,append)
    val writer1 = new java.io.OutputStreamWriter(filestream,"UTF-8")
    val writer = new java.io.PrintWriter(writer1)
    s.foreach(writer.println)
    writer.close
    writer1.close
    filestream.close
  }
  def main(args: Array[String]) {
  	val stocks="sz000758,sz000937,sz000401,sz000629,sh600729,sh600022,sz000807,sz000783,sz000793,sz002006,sz000970,sz002106,sz000100,sz000968,sz000009,sz002490,sz000792,sh600988,sz002233,sh600470,sz000691,sh600172,sz002145,sh600200,sh600885,sh600332,sh600784,sz300175,sz000007,sh600613,sz300146,sz000779,sh600520,sz002298,sh600160,sh600478,sz000030,sh600209,sz000663,sh600193,sh600466,sz002450,sh600766,sh600179,sh600381,sz000995,sz002236,sz000750,sh600300,sh600260,sz002225,sh600636,sz002456,sh600077,sh600389,sh600111,sh600645,sh600180,sz000056,sh600882,sz300104,sz002568,sz300162,sz000799,sz002070,sh600228,sh600113,sh600010,sh600696,sz000975,sh600340,sz300205,sz000752".split(",")
    val date=new java.util.Date
    val now=dateFormat.format(date)
    def da(a:Int)=now.take(8).toLong*1000000+(a*100)
    val datelist=(935.to(1500,5).filter(a=>a%100<60&&(a<1130||a>1300)).map(a=>(da(a),da(a))):+
      (da(1130),da(1300)):+(da(1136),da(1300)):+(da(1506),da(1500))).toList.sortBy(_._1)
    for(dd <- datelist.filter(_._1 > now.toLong)){
      getSinaDay(dd._1,dd._2,stocks)
    }
  }
}